package com.example.gustavo.hello.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.gustavo.hello.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Constructor;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;

/**
 * Created by gustavo on 2015/3/9.
 */
public class MainFragment extends Fragment {

    private static final String TAG = "MainFragment";
    private String[] titles = {"PhotoFallActivity","ToolbarActivity","AudioRecordActivity","SlidingTabActivity","TabHostActivity","ToolbarDrawerActivity","DragActivity",
            "CropImageActivity", "SendSMSActivity", "RxJavaActivity", "ListViewAnimationActivity", "GoPushActivity", "ListViewActivity",
            "DataBindingActivity", "MaterialDesighActivity", "RecyclerViewActivity", "HorizontalScrollViewActivity", "RealmActivity",
            "cn.limc.demo.activity.ChartListActivity", "AnimationActivity"};
    private ArrayAdapter adapter;
    private ListView listView;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        Log.w(TAG, "onAttach");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.w(TAG, "onCreate");

    }


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {


        View view = inflater.inflate(R.layout.fragment_main, null);
        listView = (ListView) view.findViewById(R.id.listView);

        adapter = new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1,titles);
        listView.setAdapter(adapter);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                try {
                    String title = titles[position];
                    String className;
                    if(title.startsWith("com.") || title.startsWith("cn.")){
                        className = title;
                    }else{
                        className = "com.example.gustavo.hello.activity." + titles[position];
                    }
                    Class<?> aClass = Class.forName(className);
                    goToActivity(aClass);

                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });




        return view;
    }


    private void goToActivity(Class clazz) {
        startActivity(new Intent(getActivity(),clazz));
    }

}
